package com.letoken.platform.repo.app.redis;

import cn.hutool.core.util.StrUtil;
import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.util.BeanUtil;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @version 1.0
 * @author： letokenlabs
 * @date： 2021/11/24 9:55
 * 用户登录凭证
 */
public class AppUserQRCodeRedis {
    private StringRedisTemplate stringRedisTemplate;

    private final HashOperations<String, String, String> valueOps;

    private AppUserQRCodeRedis() {
        stringRedisTemplate = BeanUtil.getBean(StringRedisTemplate.class);
        this.valueOps = stringRedisTemplate.opsForHash();
    }

    private enum Singleton {
        INSTANCE;
        private AppUserQRCodeRedis instance;

        // JVM会保证此方法绝对只调用一次
        Singleton() {
            instance = new AppUserQRCodeRedis();
        }
    }

    /**
     * 活动配置
     */
    String cacheKey = RedisKeyConstant.APP_USER_QR_CODE;

    public static AppUserQRCodeRedis me() {
        return AppUserQRCodeRedis.Singleton.INSTANCE.instance;
    }

    public String getValue(Integer userId) {
        return valueOps.get(cacheKey, userId.toString());
    }

    public void putValue(Integer userId, String value) {
        if (StrUtil.isNotBlank(value)){
            valueOps.put(cacheKey, userId.toString(), value);
        }
    }

}
